package com.wangdao.mall.service;

import com.wangdao.mall.dao.OrderDao;
import com.wangdao.mall.dao.OrderDaoImp;
import com.wangdao.mall.model.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author:HaiyangTian
 * @Description:
 * @Date: Created in 18:53 2020/5/3
 * @Modified By:
 */
public class OrderServiceImp implements OrderService {

    private OrderDao orderDao = new OrderDaoImp();

    /**
     * 查询出需要的响应体数据，然后赋值返回
     * @param param
     * @return
     */
    @Override
    public OrderResponse ordersByPage(OrderRequestParam param) {
        //多条件查询
        //1.根据条件查询出total
        Integer total = orderDao.getTotal(param);
        //2.根据条件查询出具体的订单数据
        List<Order> orders = orderDao.ordersByPage(param);
        //遍历orders，给每一个order中的OrderUser对象赋值
        for (Order order : orders) {
            //先创建一个新的OrderUser对象赋值
            OrderUser user = new OrderUser();
            user.setAddress(order.getAddress());
            user.setName(order.getName());
            user.setNickname(order.getNickname());
            user.setPhone(order.getPhone());
            //最后把这个对象赋值给order
            order.setUser(user);
            //另一种解决没有state状态的方法：
            //取出stateId，根据不同的id设置不同的状态
        }
        OrderResponse orderResponse = new OrderResponse();
        orderResponse.setTotal(total);
        orderResponse.setOrders(orders);
        return orderResponse;
    }

    /**
     * 将数据进行封装到orderInfo中
     * @param id
     */
    @Override
    public OrderInfo getOrder(String id) {
        CurSpec curSpec = new CurSpec();
        Curstate curstate = new Curstate();
        //将order的数据封装进orderInfo中
        Order order = orderDao.getOrder(id);
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setId(order.getId());
        orderInfo.setAmount(order.getAmount());
        orderInfo.setNum(order.getGoodsNum());
        orderInfo.setGoodsDetailId(order.getGoodsDetailId());
        orderInfo.setState(order.getStateId());
        orderInfo.setGoods(order.getGoods());
        //不能从order表中获取的数据间接查询进行封装
        List<OrderSpec> specs = orderDao.getSpecList(orderInfo.getGoodsDetailId());
        orderInfo.setSpec(specs);
        curSpec.setId(order.getGoodsDetailId());
        curstate.setId(order.getStateId());
        orderInfo.setCurSpec(curSpec);
        orderInfo.setCurState(curstate);
        return orderInfo;
    }

    @Override
    public int changeOrder(OrderUpdate update) {
        return orderDao.changeOrder(update);
    }

    @Override
    public int deleteOrder(String id) {
        return orderDao.deleteOrder(id);
    }

    @Override
    public int addOrder(Cart cart) {
        return orderDao.addOrder(cart);
    }

    /**
     * 展示订单和购物车，包装数据
     * @param state
     * @param token
     * @return
     */
    @Override
    public List<OrderList> getOrderByState(String state, String token) {
        List<Order> orderList = orderDao.getOrderByState(state);
        List<OrderList> orders = new ArrayList<>();
        for (Order list : orderList) {
            OrderList list1 = new OrderList();
            list1.setId(list.getId());
            list1.setState(list.getStateId());
            list1.setGoodsNum(list.getGoodsNum());
            list1.setAmount(list.getAmount());
            list1.setGoodsDetailId(list.getGoodsDetailId());
            list1.setCreatetime(list.getCreatetime());
            OrderGoods goods = orderDao.getGoods(orderDao.getGoodsId(list.getGoodsDetailId()));
            SpecId spec = orderDao.getSpec(list.getGoodsDetailId());
            goods.setSpec(spec.getSpecName());
            goods.setUnitPrice(spec.getUnitPrice());
            list1.setHasComment(orderDao.getHasComment(goods.getName()));
            list1.setGoods(goods);
            orders.add(list1);
        }
        return orders;
    }

    @Override
    public void settleAccounts(CartLists cartList) {
        for (CartList list : cartList.getCartList()) {
            orderDao.settleAccounts(list);
        }
    }

    @Override
    public int confirmReceive(String id) {
        return orderDao.confirmReceive(id);
    }

    @Override
    public int pay(String id) {
        return orderDao.pay(id);
    }
}
